package model

//用户

import (
	"chitchat_http/util"
	"errors"
	"github.com/doujunyu/gogo/sql_aid"
	"os"
)

const TableByUser = "self_user"

const UserByStatusNormal = 1 //正常
const UserByStatusForbid = 2 //禁用

func UserByStatusString(status int) string {
	stringMpa := map[int]string{
		UserByStatusNormal: "正常",
		UserByStatusForbid: "禁用",
	}
	return stringMpa[status]
}

// UserByInfo 个人信息
func UserByInfo(userId int64) (map[string]interface{},error) {
	userSql, args := sql_aid.PgTable(TableByUser).Field("id", "nick", "avatar","motto").Where("id = ?", userId).ToSql()
	userData, err := sql_aid.DataToMap(PgSql.Query(userSql, args...))
	if err != nil {
		return nil,err
	}
	if len(userData) == 0 {
		return nil,errors.New("空用户")
	}
	http := os.Getenv("SERVER_HTTP")
	userData[0]["avatar"] = util.IsPicturePath(userData[0]["avatar"].(string),http)
	return userData[0],nil
}

// UserByIdsToData 根据用户ID获取用户昵称和头像
func UserByIdsToData(userIds ...*[]interface{})map[int64]map[string]interface{}{
	userSlice := make([]interface{},0)
	for _, val := range userIds {
		userSlice = append(userSlice,*val...)
	}
	if len(userSlice) <= 0 {
		return map[int64]map[string]interface{}{}
	}
	userSql, args := sql_aid.PgTable(TableByUser).Field("id", "avatar", "nick","motto").WhereIn("id", userSlice...).ToSql()
	userdata, _ := sql_aid.DataToMap(PgSql.Query(userSql, args...))
	return util.ArrayColumnByInt64(&userdata, "id")
}
